import { defineStore } from "pinia";
import { computed, reactive, ref, toRefs } from "vue";
import apis from '@/apis/index';

/**
 * 出勤统计-页面-共享数据仓库
 */
export const useAttendanceStore = defineStore("attendance", () => {
  const earlyTableRef = ref([]);
  const lateTableRef = ref([]);
  const earlyBIRef = ref({});
  const lateBIRef = ref({});
  const isAttendanceLoadingRef = ref(true);
  const attendanceListRef = ref([]); // 出勤信息列表
  const totalRef = ref(0);
  const isLoadingRef = ref(true);
  /**
   * 响应式数据
   */
  const attendanceData = reactive({
    // 表格数据列表
    tableList: [  
      { title: '迟到详情', renderList: lateTableRef },
      { title: '早退详情', renderList: earlyTableRef },
    ],
    // 图表数据列表
    chartList: [
      { title: '迟到员工数量', renderList: lateBIRef },
      { title: '早退员工数量', renderList: earlyBIRef },
    ]
  });

  /**
   * 方法
   */
  // 更新数据
  async function updateState(data:any) {
    earlyTableRef.value = data.earlyTable;
    lateTableRef.value = data.lateTable;
    earlyBIRef.value = data.earlyBI;
    lateBIRef.value = data.lateBI;
  }

  function updateAttendanceList(data:any) {
    attendanceListRef.value = data.attendanceList;
    totalRef.value = data.attendanceTotal;
    isLoadingRef.value = false;
  }

  // 获取出勤统计数据
  async function getAttendanceData() {
    try {
      const data = await apis.attendance.getAttendanceTable_API();
      // console.log(attendanceData,'attendance')
      if (data) {
        // 更新数据
        await updateState(data);
        // console.log(attendanceData,'attendance')
        // 修改加载状态
        setTimeout(() => {
          isAttendanceLoadingRef.value = false;
        }, 1500);
        
      }
    } catch (error) {
      console.log(error, 'pinia获取出勤统计数据失败');
      return;
    }
  }

  async function getAttendanceList() {
    try {
      const res = await apis.attendance.getAttendanceList_API();
      console.log(res,'srrrrrr')
      if (res) {
        updateAttendanceList(res);
      }
    } catch (error) {
      console.log(error,'list-attendance');
    }
  }

  /**
   * 返回保留值
   */
  return {
    attendanceData,
    getAttendanceData,
    isAttendanceLoadingRef,
    attendanceListRef,
    getAttendanceList,
    isLoadingRef,
    totalRef
  }
});